﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormCadastroAlerta : Form
    {
        private string operacao = "";

        public FormCadastroAlerta()
        {
            InitializeComponent();
        }

        private void FormCadastroAlerta_Load(object sender, EventArgs e)
        {
            //Popular a lista de alertas cadastrados
            this.comboBoxSituacao.DataSource = AlertaVO.getSituacoes();
            List<ClienteVO> listClienteVOs = new List<ClienteVO>();
            listClienteVOs.Add(ClienteVO.CreateClienteVO(-1,""));
            listClienteVOs.AddRange(ModelBO.getClientes(true));
            this.comboBoxCliente.DataSource = listClienteVOs;
            this.dataGridViewAlertas.DataSource = ModelBO.getAlertas();
            setupButtonsControls();
        }

        private void dataGridViewAlerta_SelectionChanged(object sender, EventArgs e)
        {
            setupButtonsControls();
        }

        private void buttonNovo_Click(object sender, EventArgs e)
        {
            this.operacao = "novo";
            setupButtonsControls();
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.operacao = "cancelar";
            setupButtonsControls();
        }

        private void buttonEditar_Click(object sender, EventArgs e)
        {
            this.operacao = "editar";
            setupButtonsControls();
        }

        private void buttonExcluir_Click(object sender, EventArgs e)
        {
            this.operacao = "excluir";

            List<AlertaVO> alertaList = (List<AlertaVO>)this.dataGridViewAlertas.DataSource;
            AlertaVO alerta = alertaList[this.dataGridViewAlertas.CurrentRow.Index];
            DialogResult dr = MessageBox.Show(this, "Confirma a exclusão do alerta " + alerta.idAlerta, "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString().Equals("Yes"))
            {
                ModelBO.delAlerta(alerta);

                int nRow = this.dataGridViewAlertas.CurrentRow.Index;
                this.dataGridViewAlertas.DataSource = ModelBO.getAlertas();
                if (this.dataGridViewAlertas.Rows.Count > 0)
                {
                    dataGridViewAlertas.CurrentCell = dataGridViewAlertas.Rows[Math.Min(nRow,this.dataGridViewAlertas.Rows.Count-1)].Cells[0];
                    dataGridViewAlertas.Rows[Math.Min(nRow, this.dataGridViewAlertas.Rows.Count-1)].Selected = true;
                }
                setupButtonsControls();
            }
        }

        private void buttonSalvar_Click(object sender, EventArgs e)
        {
            String erros = "";
            if (this.textBoxAlerta.Text.Trim().Length == 0)
                erros = erros + "Alerta deve ser informado.\r\n";
            if (this.textBoxPlaca.Text.Trim().Length == 0 && (int)this.comboBoxCliente.SelectedValue == -1)
                erros = erros + "Cliente ou Placa deve ser informado.\r\n";
            
            if (erros.Length > 0)
            {
                MessageBox.Show(this, erros, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            AlertaVO alerta = null;

            if (this.operacao.Equals("novo"))
            {

                alerta = AlertaVO.CreateAlertaVO();
                alerta.mensagem = this.textBoxAlerta.Text;
                alerta.placa = this.textBoxPlaca.Text;
                alerta.idCliente = (int)this.comboBoxCliente.SelectedValue;
                alerta.apenasComunicar = this.radioButtonApenasComunicar.Checked;
                alerta.impedirEntrada = this.radioButtonImpedirEntrada.Checked;
                alerta.reterSaida = this.radioButtonReterSaida.Checked;
                alerta.situacao = (string)this.comboBoxSituacao.SelectedValue;
                if (alerta.situacao.Equals(AlertaVO.Situacao.Executado)){
                    alerta.dataExec = this.dateTimePickerExecucao.Value;
                    alerta.idCabine = -1;
                }

                alerta.idAlerta = (int)ModelBO.newAlerta(alerta);
            }
            if (this.operacao.Equals("editar"))
            {
                List<AlertaVO> alertaList = (List<AlertaVO>)this.dataGridViewAlertas.DataSource;
                alerta = alertaList[this.dataGridViewAlertas.CurrentRow.Index];

                alerta.mensagem = this.textBoxAlerta.Text;
                alerta.placa = this.textBoxPlaca.Text;
                alerta.idCliente = (int)this.comboBoxCliente.SelectedValue;
                alerta.apenasComunicar = this.radioButtonApenasComunicar.Checked;
                alerta.impedirEntrada = this.radioButtonImpedirEntrada.Checked;
                alerta.reterSaida = this.radioButtonReterSaida.Checked;
                alerta.situacao = (string)this.comboBoxSituacao.SelectedValue;
                if (alerta.situacao.Equals(AlertaVO.Situacao.Executado))
                {
                    alerta.dataExec = this.dateTimePickerExecucao.Value;
                    alerta.idCabine = -1;
                }
                else
                {
                    alerta.dataExec = null;
                    alerta.idCabine = null;
                }

                ModelBO.setAlerta(alerta);
            }
            this.operacao = "salvar";

            List<AlertaVO> listAlertaVOs = ModelBO.getAlertas();
            dataGridViewAlertas.DataSource = listAlertaVOs;
            int nRow = listAlertaVOs.FindIndex(a => a.idAlerta == alerta.idAlerta);
            dataGridViewAlertas.CurrentCell = dataGridViewAlertas.Rows[nRow].Cells[0];
            dataGridViewAlertas.Rows[nRow].Selected = true;
            setupButtonsControls();
        }


        private void setupButtonsControls()
        {
            bool haRows = this.dataGridViewAlertas.Rows.Count > 0 && this.dataGridViewAlertas.CurrentRow!=null;
            bool novo = this.operacao.Equals("novo");
            bool edit = this.operacao.Equals("editar");
            bool canc = this.operacao.Equals("cancelar");

            this.buttonCancelar.Enabled = novo || edit;
            this.buttonNovo.Enabled = !novo && !edit;
            this.buttonEditar.Enabled = haRows && !novo && !edit;
            this.buttonExcluir.Enabled = haRows && !novo && !edit;
            this.buttonSalvar.Enabled = novo || edit;

            this.dataGridViewAlertas.Enabled = !novo && !edit;

            this.textBoxAlerta.Enabled = novo || edit;
            this.textBoxId.Enabled = novo || edit;
            this.textBoxAlerta.Enabled = novo || edit;
            this.textBoxPlaca.Enabled = novo || edit;
            this.comboBoxCliente.Enabled = novo || edit;
            this.comboBoxSituacao.Enabled = novo || edit;
            this.radioButtonReterSaida.Enabled = novo || edit;
            this.radioButtonImpedirEntrada.Enabled = novo || edit;
            this.radioButtonApenasComunicar.Enabled = novo || edit;
            this.dateTimePickerExecucao.Enabled = novo || edit;

            if (!haRows || novo)
            {
                this.textBoxId.Text = "";
                this.textBoxAlerta.Text = "";
                this.textBoxPlaca.Text = "";
                this.comboBoxCliente.SelectedValue = -1;
                this.comboBoxSituacao.SelectedValue = AlertaVO.Situacao.Pendente;
                this.dateTimePickerExecucao.Visible = false;
                this.labelExecucao.Visible = false;
            }
            else {
                List<AlertaVO> alertaList = (List<AlertaVO>)this.dataGridViewAlertas.DataSource;
                AlertaVO alerta = alertaList[this.dataGridViewAlertas.CurrentRow.Index];
                this.textBoxId.Text = alerta.idAlerta.ToString();
                this.textBoxAlerta.Text = alerta.mensagem;
                this.textBoxPlaca.Text = alerta.placa;
                this.comboBoxCliente.SelectedValue = alerta.idCliente;
                this.comboBoxSituacao.SelectedValue = alerta.situacao;
                if (alerta.dataExec!=null) this.dateTimePickerExecucao.Value = alerta.dataExec.Value;
                this.radioButtonReterSaida.Checked = alerta.reterSaida;
                this.radioButtonImpedirEntrada.Checked = alerta.impedirEntrada;
                this.radioButtonApenasComunicar.Checked = alerta.apenasComunicar;
            }
            if (((string)this.comboBoxSituacao.SelectedValue).Equals(AlertaVO.Situacao.Executado))
            {
                this.dateTimePickerExecucao.Visible = true;
                this.labelExecucao.Visible = true;
            }
            else
            {
                this.dateTimePickerExecucao.Visible = false;
                this.labelExecucao.Visible = false;
            }
           if (novo || edit){
                this.textBoxAlerta.Focus();
            }
        }

        private void dataGridViewAlertas_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            foreach (DataGridViewRow dgvr in dgv.Rows)
            {
                AlertaVO alertaVO = (AlertaVO)dgvr.DataBoundItem;
                if (alertaVO.idCliente != -1)
                {
                    ClienteVO clienteVO = ModelBO.getCliente(alertaVO.idCliente);
                    if (clienteVO != null) dgvr.Cells["cliente"].Value = clienteVO.nome;
                }
            }

        }

        private void comboBoxSituacao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((string)this.comboBoxSituacao.SelectedValue).Equals(AlertaVO.Situacao.Executado))
            {
                this.dateTimePickerExecucao.Value = DateTime.Now;

                this.labelExecucao.Visible = true;
                this.dateTimePickerExecucao.Visible = true;
            }
            else
            {
                this.labelExecucao.Visible = false;
                this.dateTimePickerExecucao.Visible = false;
            }
        }

     }
}
